﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Script.Serialization;
using System.Xml.Linq;
using System.Collections.Generic;

namespace SuuntoWebsite.Events._201211.Handler
{
    public partial class Event : System.Web.UI.Page
    {
        private static string sqlCon = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString_Event"].ConnectionString;
        private static string webDomain = System.Configuration.ConfigurationManager.AppSettings["WebDomain"];
        protected string beginTimeStr = System.Configuration.ConfigurationManager.AppSettings["begintime_1211"];
        protected string endTimeStr = System.Configuration.ConfigurationManager.AppSettings["endtime_1211"];

        protected void Page_Load(object sender, EventArgs e)
        {
            sqlCon = Public.DataProtector.EncryptString("Data Source=C874F1768225439;Initial Catalog=sq_suunto;Integrated Security=True");
            Response.Expires = -1;
            string fun = Request["fun"];
            string city = Request["city"];
            switch (fun)
            {
                case "city": { GetCityList(); break; }
                case "storename": { GetStoreName(); break; }
                case "store": { GetStore(); break; }
                case "check": { CheckForm(); break; }
            }

        }
        protected void GetCityList()
        {
           var list= new Bll.DALTStore().TStoreCity(sqlCon);
           Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
           Response.Write(new JavaScriptSerializer().Serialize(list));
        }
        protected void GetStoreName()
        {
            string city = Request["city"];
            var list = new Bll.DALTStore().TStoreName(sqlCon, city);
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            Response.Write(new JavaScriptSerializer().Serialize(list));
        }
        protected void GetStore()
        {
            int pkid = int.Parse(Request["pkid"]);
            var list = new Bll.DALTStore().TStore(sqlCon, pkid);
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            Response.Write(new JavaScriptSerializer().Serialize(list));
        }

        protected void CheckForm()
        {
            if (CheckTime())
            {
                var formModel = new Model.TAccountForm();
                var logModel = new Model.TCheckLog();
                var codeModel = new Model.TProductCode();

                formModel.PKID = new Bll.DALTAccountForm().GetMaxID(sqlCon);
                formModel.UserName = Request["username"];
                formModel.CreateTime = DateTime.Now;
                formModel.Email = Request["email"];
                formModel.Model = Request["model"];
                formModel.Phone = Request["phone"];
                formModel.SerialNumber = Request["code"];
                formModel.StoreID = int.Parse(Request["store"]);
                formModel.UpdateTime = DateTime.Now;
                formModel.IPAddress = getIp();
                formModel.CheckState = false;
                formModel.CheckMessage = "验证";

                logModel.UserID = formModel.PKID;
                logModel.SerialNumber = formModel.SerialNumber;
                logModel.Phone = formModel.Phone;
                logModel.CheckTime = DateTime.Now;
                logModel.CheckState = false;
                logModel.CheckMessage = "验证";

                if (new Bll.DALTCheckLog().TCheckLogExistsByPhone(sqlCon, formModel.Phone, Convert.ToDateTime(beginTimeStr)))
                {
                    codeModel = new Bll.DALTProductCode().TProductCode(sqlCon, formModel.SerialNumber);
                    if (codeModel.PKID != 0)
                    {
                        if (!codeModel.Checked.Value)
                        {
                            formModel.CheckState = true;
                            formModel.CheckMessage = "验证成功";

                            logModel.CheckState = true;
                            logModel.CheckMessage = "验证成功";

                            new Bll.DALTAccountForm().TAccountFormAdd(sqlCon, formModel);
                            new Bll.DALTCheckLog().TCheckLogAdd(sqlCon, logModel);
                            new Bll.DALTProductCode().TProductCodeUpdate(sqlCon, formModel.SerialNumber);

                            Response.Write(new JavaScriptSerializer().Serialize(new List<object>() { "0" }));
                        }
                        else
                        {
                            formModel.CheckMessage = "产品编号已申请过";

                            logModel.CheckMessage = formModel.CheckMessage;

                            var storeModel = new Bll.DALTStore().TStore(sqlCon, formModel.SerialNumber);

                            new Bll.DALTAccountForm().TAccountFormAdd(sqlCon, formModel);
                            new Bll.DALTCheckLog().TCheckLogAdd(sqlCon, logModel);
                            Response.Write(new JavaScriptSerializer().Serialize(new List<object>() { "3", storeModel.City + "," + storeModel.StoreName + "," + storeModel.Address }));
                        }
                    }
                    else
                    {
                        formModel.CheckMessage = "产品编号不存在";
                        logModel.CheckMessage = formModel.CheckMessage;
                        new Bll.DALTAccountForm().TAccountFormAdd(sqlCon, formModel);
                        new Bll.DALTCheckLog().TCheckLogAdd(sqlCon, logModel);
                        Response.Write(new JavaScriptSerializer().Serialize(new List<object>() { "1" }));
                    }
                }
                else
                {
                    formModel.CheckMessage = "错误验证超过允许次数";
                    logModel.CheckMessage = formModel.CheckMessage;
                    new Bll.DALTAccountForm().TAccountFormAdd(sqlCon, formModel);
                    new Bll.DALTCheckLog().TCheckLogAdd(sqlCon, logModel);
                    Response.Write(new JavaScriptSerializer().Serialize(new List<object>() { "2" }));
                }
            }
        }


        protected bool CheckTime()
        {
            bool flag = false;
            DateTime nowTime = DateTime.Now;
            DateTime beginTime = Convert.ToDateTime(beginTimeStr);
            DateTime endTime = Convert.ToDateTime(endTimeStr);
            if (nowTime.CompareTo(beginTime) > 0 && nowTime.CompareTo(endTime) < 0)
            {
                flag = true;
            }
            return flag;
        }
        /// 获得客户端IP
        /// 
        /// <returns></returns>
        private string getIp()
        {
            // 穿过代理服务器取远程用户真实IP地址
            string Ip = string.Empty;
            if (Request.ServerVariables["HTTP_VIA"] != null)
            {
                if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
                {
                    if (Request.ServerVariables["HTTP_CLIENT_IP"] != null)
                        Ip = Request.ServerVariables["HTTP_CLIENT_IP"].ToString();
                    else
                        if (Request.ServerVariables["REMOTE_ADDR"] != null)
                            Ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
                        else
                            Ip = "127.0.0.1";
                }
                else
                    Ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else if (Request.ServerVariables["REMOTE_ADDR"] != null)
            {
                Ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            else
            {
                Ip = "127.0.0.1";
            }
            return Ip;
        }
    }
}
